package com.helper.easyHelper.utils;

import com.helper.easyHelper.common.entity.Table;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.logging.log4j.util.PropertiesUtil;

import java.io.*;
import java.util.Map;


/**
 * @author 狠穿
 * @version 1.0.0
 * @date 2023/1/29
 */
public class FreeMakerUtil {


    public static void generateFile(Table table,String outputDir,String templateName,String fileName){
        String modelPackage = PropertiesUtil.getProperties().getStringProperty("model");
        table.setModelPackage("com.baosight.ems");
        Configuration configuration = new Configuration();
        Writer out = null;
        String templateDir = "src/main/resources/template";
        try {
            configuration.setDirectoryForTemplateLoading(new File(templateDir));
            Template template = configuration.getTemplate(templateName);
            File docFile = new File(outputDir+"\\"+fileName+".java");
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(docFile)));
            template.process(table,out);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        }
    }
}
